package com.xudow.app.ui.user;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.activeshare.edu.ucenter.models.base.Activities;
import com.activeshare.edu.ucenter.models.base.Courses;
import com.activeshare.edu.ucenter.models.user.UserFavoriteItemsWithOther;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.ui.BaseFragment;
import com.xudow.app.ui.adapter.user.FavoriteActivityAdapter;
import com.xudow.app.ui.adapter.user.FavoriteCourseAdapter;
import com.xudow.app.ui.support.RecyclerViewScrollLocationListener;
import com.xudow.app.ui.support.XLinearLayoutManager;
import com.xudow.app.ui.task.user.FavoriteListTask;
import com.xudow.app.util.Maps;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 我的收藏(活动)ListFragment
 *
 * @author lukecheng
 */
public class FavoriteActivityListFragment extends BaseFragment
        implements FavoriteActivityAdapter.OnRecyclerViewListener {
    private static final int TYPE_ACTIVITY = 1;  //收藏类型为活动

    private List<UserFavoriteItemsWithOther> favorites;

    private FavoriteActivityAdapter favoriteAdapter;

    private RecyclerView favoriteRecyclerView;
    private ProgressBar progressBar;

    private int currentType = TYPE_ACTIVITY;
    private int page = 1;

    private FavoriteListTask favoriteListTask;

    /**
     * 加载收藏
     */
    public void loadFavorite() {
        HashMap<String, String> params = Maps.newHashMap();
        params.put("type", "" + currentType);
        params.put(Config.PAGINATION_PARAM_NAME, "" + page);

        favoriteListTask = new FavoriteListTask(getActivity(), favoriteListHandler);
        favoriteListTask.execute(params);
    }

    /**
     * 收藏数据Handler
     */
    private Handler favoriteListHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            hideLoadingDialog();

            if (FavoriteListTask.SUCCESS == msg.what) {
                UserFavoriteItemsWithOther[] data =
                        (UserFavoriteItemsWithOther[]) msg.getData().getSerializable("favorites");

                favorites.addAll(Arrays.asList(data));

                if (data.length <= 0) {
                    page--;
                }

                progressBar.setVisibility(View.GONE);
                favoriteRecyclerView.setVisibility(View.VISIBLE);

                favoriteAdapter.setOnRecyclerViewListener(FavoriteActivityListFragment.this);
                favoriteAdapter.notifyDataSetChanged();
            } else {
                if (page > 1) {
                    page--;
                }
            }
        }
    };

    static FavoriteActivityListFragment newInstance() {
        FavoriteActivityListFragment f = new FavoriteActivityListFragment();
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_favorite_activity_list, container, false);

        progressBar = (ProgressBar) v.findViewById(R.id.progressbar);

        favoriteRecyclerView = (RecyclerView) v.findViewById(R.id.data_list);
        favoriteRecyclerView.setHasFixedSize(true);

        XLinearLayoutManager layoutManager = new XLinearLayoutManager(getActivity());
        layoutManager.setOnRecyclerViewScrollLocationListener(favoriteRecyclerView,
                new RecyclerViewScrollLocationListener() {
                    @Override
                    public void onTopWhenScrollIdle(RecyclerView recyclerView) {
                    }

                    @Override
                    public void onBottomWhenScrollIdle(RecyclerView recyclerView) {
                        //设置页数
                        page++;
                        loadFavorite();
                    }
                });
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        favoriteRecyclerView.setLayoutManager(layoutManager);

        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        favorites = new ArrayList<UserFavoriteItemsWithOther>();

        favoriteAdapter = new FavoriteActivityAdapter(favorites);
        favoriteAdapter.setOnRecyclerViewListener(this);
        favoriteRecyclerView.setAdapter(favoriteAdapter);

        loadFavorite();
    }

    @Override
    public void onItemClick(int position) {
        //TODO:打开活动详细页面
    }

    @Override
    public boolean onItemLongClick(int position) {
        return false;
    }

    /**
     * 删除收藏处理
     *
     * @param position
     */
    @Override
    public void onDelete(final int position) {
        String message = getString(R.string.delete_confirmation);
        AlertDialog dlg = new AlertDialog.Builder(getActivity())
                .setMessage(message)
                .setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //TODO:删除收藏
                        favorites.remove(position);
                        favoriteAdapter.notifyDataSetChanged();
                    }
                })
                .create();
        dlg.show();
    }

    @Override
    public void onDestroy() {
        if (null != favoriteListTask && !favoriteListTask.isCancelled()) {
            favoriteListTask.cancel(true);
            favoriteListTask = null;
        }

        super.onDestroy();
    }
}